<?php 
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id$
* @package VirtueMart
* @subpackage html
* @copyright Copyright (C) 2010 Forrest Chamberlain. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/

/**
 * In this page, we display:
 *  - all existing wishlists -- ordered from newest to oldest
 *  OR
 *  - the contents of a wishlist, if a wishlist_id is present
 */
mm_showMyFileName( __FILE__ );


if ($perm->check( "admin,storeadmin" ))
{
	require_once( CLASSPATH . 'ps_wishlist.php' );
	require_once( CLASSPATH . "pageNavigation.class.php" );
	require_once( CLASSPATH . "htmlTools.class.php" );

	$wishlist_prod_id = JRequest::getInt('wishlist_prod_id');
	$func = JRequest::getInt('func');

	$result = false;
	$result = show_common_item_users( $wishlist_prod_id, $limitstart, $limit );
}
else
{
global $mainframe;
	$mainframe->redirect( URL . 'index.php', $GLOBALS['VM_LANG']->_('PHPSHOP_MOD_NO_AUTH') );
}

/* ----------- */
/**
 * Show users of common wishlist item
 * @author forrest
 * @param int $limitstart
 * @param int $limit
 * @return boolean -- successful or not
 */
function show_common_item_users( $wishlist_prod_id, $limitstart, $limit )
{
	global $VM_LANG, $modulename, $sess, $ps_html;

	$p_row = ps_wishlist::get_product_info( $wishlist_prod_id );
	$product_name = $p_row['product_name'];
	$count_users = ps_wishlist::get_common_wishlist_item_users_count($wishlist_prod_id, $GLOBALS['keyword']);
	$num_rows = count( $count_users ) ;
	$item_users = ps_wishlist::get_common_wishlist_item_users($wishlist_prod_id, $GLOBALS['keyword'], $limitstart, $limit);

	if ($item_users !== false)
	{
		// Create the Page Navigation
		$pageNav = new vmPageNav( $num_rows, $limitstart, $limit );

		// Create the List Object with page navigation
		$listObj = new listFactory( $pageNav );

		$listObj->writeSearchHeader("Users Who Added $product_name", IMAGEURL."ps_image/wishlist.png", $modulename, "item_users");

		// start the list table
		$listObj->startTable();

		// these are the columns in the table
		$columns = Array (
			"#" => "width=\"5%\"", 
			"<input type=\"checkbox\" name=\"toggle\" value=\"\" onclick=\"checkAll(".$num_rows.")\" />" => "width=\"5%\"",
			'User ID' => 'width="5%"',
			$VM_LANG->_('PHPSHOP_WISHLIST_USER_LBL') => 'width="40%"',
			'Email User' => 'width="40%"',
		);

		$listObj->writeTableHeader( $columns );

		$i = 0;
		if (count( $item_users ) > 0)
		{
			foreach ($item_users as $u)
			{
				$listObj->newRow();
	
				// The row number
				$listObj->addCell( $pageNav->rowNumber( $i ) );
	
				// The Checkbox
				$listObj->addCell( vmCommonHTML::idBox( $i, $u['user_id'], false, "user_id" ) );
		
				$listObj->addCell( $u['user_id'] );
	
				$url = $_SERVER['PHP_SELF'] . "?pshop_mode=admin&page=admin.user_form&user_id=" . $u['user_id'];
				$listObj->addCell( '<a href="' . $sess->url( $url ) . '">' . $u['first_name'] . ' ' . $u['last_name'] . '</a><br />' );

				$url = 'mailto:'.$u['user_email'];
				$listObj->addCell( '<a href="' .$url. '">' . $u['user_email'] . '</a><br />' );
		
				$i++;
			}
		}

		$listObj->writeTable();

		$listObj->endTable();

		$listObj->writeFooter( $GLOBALS['keyword'] );

		return true;
	}

	return false;
}